package com.ityu.myapi.controller;


import com.ityu.common.utils.Utils;
import com.ityu.common.vo.front.Ret;
import com.ityu.common.vo.front.Rets;
import com.ityu.myapi.service.StudyService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/studyOne")
@Slf4j
public class StudyOneController {

    final JavaMailSenderImpl javaMailSender;
    final StudyService studyService;

    final  RedisTemplate redisTemplate;

    public StudyOneController(JavaMailSenderImpl javaMailSender, StudyService studyService, RedisTemplate redisTemplate) {
        this.javaMailSender = javaMailSender;
        this.studyService = studyService;
        this.redisTemplate = redisTemplate;
    }

    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({ // 参数说明
            @ApiImplicitParam(name = "files", paramType = "query", dataType = "File")
    })
    @PostMapping(value = "/upLoad")
    public Ret<List<String>> upLoad(@RequestParam MultipartFile[] files, HttpServletRequest request) {
        List<String> names = new ArrayList<>();
        String realPath = request.getServletContext().getRealPath("/upload");
        if (!new File(realPath).exists()) {
            new File(realPath).mkdir();
        }
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            try {
                file.transferTo(new File(realPath + "/" + originalFilename));
                names.add(realPath + "/" + originalFilename);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Rets.success(names);
    }


    @ApiOperation(value = "发送email")
    @ApiImplicitParams({ // 参数说明
            @ApiImplicitParam(name = "content", paramType = "query", dataType = "string")
    })
    @PostMapping(value = "/sendEmail")
    public Ret<List<String>> sendEmail(@RequestParam String content) throws MessagingException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("你好啊");
        message.setFrom("1136081721@qq.com");
        message.setText(content);
        message.setTo("bbfx89625@163.com");
        javaMailSender.send(message);

//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
//        helper.setText("<p style='color:red'>感谢有你</p>");
//        helper.addAttachment();

        return Rets.success("发送成功");
    }


    @ApiOperation(value = "测试异步")
    @GetMapping(value = "/testAsync")
    public Ret<String> testAsync() {
        studyService.testA();
        Utils.myLog("=====testAsync==="+Thread.currentThread().getName());
        return Rets.success("测试成功");
    }

    @ApiOperation(value = "测试redis")
    @GetMapping(value = "/testRedis")
    public Ret<String> testRedis(@RequestParam String content) {
        redisTemplate.opsForValue().set("a",content);
        return Rets.success(redisTemplate.opsForValue().get("a"));
    }


    @ApiOperation(value = "测试log")
    @GetMapping(value = "/testLog")
    public Ret<String> testLog(@RequestParam String name,@RequestParam String content) {
        log.info("{}输入的内容为{}",name,content);
        return Rets.success(content);
    }

}
